package com.mydesign.demo.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import com.mydesign.demo.bean.*;
import com.mydesign.demo.mapper.StoreCommentMapper;
import com.mydesign.demo.mapper.StoreMapper;
import com.mydesign.demo.mapper.StoreToAssignmentMapper;
import com.mydesign.demo.mapper.StoreUserFollowMapper;
import com.mydesign.demo.model.ReturnResult;
import com.mydesign.demo.service.StoreService;
import com.mydesign.demo.util.RandomUtil18;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreToAssignmentMapper assignmentMapper;
    @Autowired
    StoreCommentMapper storeCommentMapper;
    @Autowired
    private StoreUserFollowMapper storeUserFollowMapper;
    
    @Override
    public ReturnResult StoreList(HttpServletRequest request) {
        StoreExample storeExample=new StoreExample();
        List<Store> stores = storeMapper.selectByExample(storeExample);
        if(stores.size()<=0) return new ReturnResult("-1","暂无任何派小店");
        List<Map> allStoreList=new ArrayList<>();
        for(Store e:stores){
            Map<String, Object> storeMap=new HashMap<>();
            storeMap.put("id",e.getId());
            storeMap.put("userid",e.getUserId());
            storeMap.put("imgCase",e.getImgCase());
            storeMap.put("imgShop",e.getImgShop());
            String shopName = e.getShopName();
            char[] chars = shopName.toCharArray();
            String s="";
            if(chars.length>5){
                for(int i=0;i<3;i++){
                    s=s+chars[i];
                }
                storeMap.put("shopName",s+"...");
            }
            else {
                storeMap.put("shopName",e.getShopName());
            }
            storeMap.put("otherview",e.getOtherview());
            storeMap.put("scope",e.getScope());
            allStoreList.add(storeMap);
        }
        return ReturnResult.dataSuccess(allStoreList);
    }

    @Override
    public ReturnResult AddStore(Store store) {
        if(StringUtils.isEmpty(store.getUserId())) return new ReturnResult("-1","未授权登录");
        StoreExample storeExample=new StoreExample();
        storeExample.createCriteria().andUserIdEqualTo(store.getUserId());
        List<Store> stores = storeMapper.selectByExample(storeExample);
        if(stores.size()>0)return new ReturnResult("-1","请勿重复申请派小店");
        store.setTime(new Date());
        store.setId(RandomUtil18.Number18());
        store.setStatus(0);
        store.setClasses(0);
        store.setOtherview(0);
        store.setStar(0);

        int a= storeMapper.insertSelective(store);
        if(a>0){
            return ReturnResult.dataSuccess(store.getId());
        }
        else {
            return ReturnResult.error();
        }
    }

    @Override
    public ReturnResult SelectStoreDetail(HttpServletRequest request) {
        String storeid = request.getParameter("storeid");
        Store store = storeMapper.selectByPrimaryKey(storeid);
        store.setOtherview(store.getOtherview()+1);
        storeMapper.updateByPrimaryKeySelective(store);
        Map<String, Object> map = BeanUtil.beanToMap(store);
        StoreCommentExample storeCommentExample=new StoreCommentExample();
        storeCommentExample.createCriteria().andStoreIdEqualTo(storeid);
        List<StoreComment> storeComments = storeCommentMapper.selectByExample(storeCommentExample);
        double server=0.0;//服务
        double economy=0.0;//质量
        double attitude=0.0;//态度
        for (StoreComment e:storeComments) {
            economy += e.getScore01();
            attitude += e.getScore02();
            server += e.getScore03();
        }
        double all=(economy+attitude+server)/storeComments.size();
        map.put("all",all);
        return ReturnResult.dataSuccess(map);
    }

    @Override
    public ReturnResult OrderStore(StoreToAssignment assignment) {
        if(StringUtils.isEmpty(assignment.getUserId()))return new ReturnResult("-1","暂未授权登录");
        assignment.setAssignmentId(RandomUtil18.Number18());
        if(StringUtils.isEmpty(assignment.getStoreId()))
            assignment.setStatus(6);
        else
            assignment.setStatus(0);
        assignment.setCostFilm(0.0);
        assignment.setCostService(0.0);
        assignment.setSecurityMoney(0);
        assignment.setCostSecurity(0.0);
        //assignment.setBusiness("自由拍摄");
        assignment.setTravelExpense(0);
        int i = assignmentMapper.insertSelective(assignment);
        if(i>0) return ReturnResult.success();
        else return ReturnResult.error();
    }

    @Override
    public ReturnResult getUserCurrentOrder(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        StoreExample storeExample=new StoreExample();
        storeExample.createCriteria().andUserIdEqualTo(userId);
        List<Store> stores = storeMapper.selectByExample(storeExample);
        Map<String, String> map=new HashMap<>();
        if(stores.size()>0){
            Store store = stores.get(0);
            StoreToAssignmentExample assignmentExample=new StoreToAssignmentExample();
            assignmentExample.createCriteria().andStoreIdEqualTo(store.getId()).andStatusEqualTo(1);
            List<StoreToAssignment> storeToAssignments = assignmentMapper.selectByExample(assignmentExample);
            if(storeToAssignments.size()>0){
                StoreToAssignment storeToAssignment = storeToAssignments.get(0);
                map.put("id",storeToAssignment.getAssignmentId());
                return ReturnResult.dataSuccess(map);
            }else {
                map.put("id","暂未接单");
                return ReturnResult.dataSuccess(map);
            }
        }else {
            map.put("id","暂无小店");
            return ReturnResult.dataSuccess(map);
        }
        //return ReturnResult.error();
    }

    @Override
    @Transactional
    public ReturnResult updateStore(Store store) {
        StoreExample storeExample=new StoreExample();
       storeExample.createCriteria().andUserIdEqualTo(store.getUserId());
        List<Store> stores = storeMapper.selectByExample(storeExample);
        Store store1 = stores.get(0);
        if(StringUtils.isNotEmpty(store.getShopName()))
        store1.setShopName(store.getShopName());
        if(StringUtils.isNotEmpty(store.getScope()))
        store1.setScope(store.getScope());
        if(StringUtils.isNotEmpty(store.getPresent()))
        store1.setPresent(store.getPresent());
        if(StringUtils.isNotEmpty(store.getImgAuth()))
        store1.setImgAuth(store.getImgAuth());
        if(StringUtils.isNotEmpty(store.getImgCase()))
        store1.setImgCase(store.getImgCase());
        if(StringUtils.isNotEmpty(store.getImgShop()))
        store1.setImgShop(store.getImgShop());
        //store1.setId(RandomUtil18.Number18());
        storeMapper.deleteByExample(storeExample);
//        StoreExample storeExample=new StoreExample();
//        storeExample.createCriteria().andUserIdEqualTo(store.getUserId());
//        List<Store> stores = storeMapper.selectByExample(storeExample);
//        if(stores.size()>0)return new ReturnResult("-1","请勿重复申请派小店");
//        store.setTime(new Date());
//        store.setId(RandomUtil18.Number18());
//        store.setStatus(0);
//        store.setClasses(0);
        //store.setOtherview(0);
        //store.setStar(0);

        int a= storeMapper.insertSelective(store1);
        if(a>0){
            return ReturnResult.dataSuccess(store1.getId());
        }
        else {
            return ReturnResult.errortRollback("-1");
        }
    }

    @Override
    public ReturnResult collect(HttpServletRequest request) {
        String storeId = request.getParameter("storeId");
        String userId = request.getParameter("userId");
        String assignmentId = request.getParameter("assignmentId");
        StoreToAssignment storeToAssignment = assignmentMapper.selectByPrimaryKey(assignmentId);
        storeToAssignment.setStatus(8);

        StoreUserFollow storeUserFollow=new StoreUserFollow();
        storeUserFollow.setUserId(userId);
        storeUserFollow.setStoreId(storeId);
        int insert = storeUserFollowMapper.insert(storeUserFollow);
        if (insert>0){

            int i = assignmentMapper.updateByPrimaryKey(storeToAssignment);
            if(i>0)
            return ReturnResult.dataSuccess("收藏成功");
            else return ReturnResult.error();
        }
        else return ReturnResult.error();
    }

    @Override
    public ReturnResult getcollect(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        StoreUserFollowExample storeUserFollowExample=new StoreUserFollowExample();
        storeUserFollowExample.createCriteria().andUserIdEqualTo(userId);
        List<StoreUserFollow> storeUserFollows = storeUserFollowMapper.selectByExample(storeUserFollowExample);
        List<Store> list=new ArrayList<>();
        for(StoreUserFollow e:storeUserFollows){
            Store store = storeMapper.selectByPrimaryKey(e.getStoreId());
            list.add(store);
        }
        return ReturnResult.dataSuccess(list);
    }
}
